package top.canyue.eca_manager.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import top.canyue.eca_manager.bean.Result;
import top.canyue.eca_manager.service.ConferenceDetailService;

import java.util.List;


/**
 * 会议详情RESTful API
 *
 * @author canyue
 */

@RestController
@RequestMapping(value = "/api/ConferenceDetail")
public class ConferenceDetailController {
    @Autowired
    ConferenceDetailService conferenceDetailService;

    /**
     * 累加缓存中的数据，准确说是计数
     *
     * @param conferenceId 会议ID
     * @param userId       用户ID
     * @param emotion      情绪，可以是 happy、sad、angry、surprise、fear、disgust、neutral
     */
    @RequestMapping(value = "/incrCache", method = RequestMethod.POST)
    public Result incrCache(@RequestParam String conferenceId, @RequestParam String userId, @RequestParam String emotion) {
        return conferenceDetailService.incrCache(conferenceId, userId, emotion);
    }

    /**
     * 获取缓存
     *
     * @param conferenceId 会议ID
     * @param userId       用户ID
     */
    @RequestMapping(value = "/getCache", method = RequestMethod.GET)
    public Result getCache(@RequestParam String conferenceId, @RequestParam String userId) {
        return conferenceDetailService.getCache(conferenceId, userId);
    }

    /**
     * 持久化整个会议的缓存
     * 本接口涉及三条以及上SQL查询，后续看看能不能优化
     * @param conferenceId 会议ID
     * @return 持久化操作执行结果
     */

    @RequestMapping(value = "/persistentAllCacheByConferenceId", method = RequestMethod.POST)
    public Result persistentAllCacheByConferenceId(@RequestParam String conferenceId) {
        return conferenceDetailService.persistentAllCacheByconferenceId(conferenceId);
    }

    /**
     * 持久化缓存(本接口已弃用)
     * @deprecated
     * @param conferenceId 会议ID
     * @param userId       用户ID
     * @return 返回内容这种包含本条记录的UUID, 本UUID由conferenceId+userId经过MD5算法生成，具有唯一性、可逆性，若需要可逆向得到这两个ID
     */
    @RequestMapping(value = "/persistentCache", method = RequestMethod.POST)
    public Result persistentCache(@RequestParam String conferenceId, @RequestParam String userId) {
        return conferenceDetailService.persistentCache(conferenceId, userId);
    }

    /**
     * 获取持久化的会议详情
     * @param id UUID
     */
    @RequestMapping(value = "/getDetailById",method = RequestMethod.GET)
    public Result getDetailById(@RequestParam String id){
        return conferenceDetailService.getDetailById(id);
    }

    /**
     * 加入会议
     * 加入的用户，立即在Detail表中创建条目，情绪计数为0
     * @param conferenceId 会议ID
     * @param userId       用户ID
     * @return 新Detail条目的UUID
     */
    @RequestMapping(value = "/joinConference",method = RequestMethod.POST)
    public Result joinConference(@RequestParam String conferenceId, @RequestParam String userId) {
        return conferenceDetailService.joinConference(conferenceId, userId);
    }

    /**
     * 批量加入会议
     *
     * @param conferenceId 会议ID
     * @param userIdList   用户ID列表
     * @return 若有失败，将返回失败用户ID
     */
    @RequestMapping(value = "/joinConferenceByAllUser", method = RequestMethod.POST)
    public Result joinConferenceByAllUser(@RequestParam String conferenceId, @RequestParam List<String> userIdList) {
        return conferenceDetailService.joinConference(conferenceId, userIdList);
    }

    /**
     * 批量移除会议中的用户
     *
     * @param userIdList   用户ID列表
     * @param conferenceId 会议ID
     */
    @RequestMapping(value = "/leaveConferenceByAllUser", method = RequestMethod.POST)
    public Result leaveConferenceByAllUser(@RequestParam List<String> userIdList, @RequestParam String conferenceId) {
        return conferenceDetailService.leaveConferenceByAllUser(userIdList, conferenceId);
    }

    /**
     * 将某个用户移除会议
     *
     * @param userId       用户ID
     * @param conferenceId 会议ID
     */
    @RequestMapping(value = "/leaveConference", method = RequestMethod.POST)
    public Result leaveConference(@RequestParam String userId, @RequestParam String conferenceId) {
        return conferenceDetailService.leaveConference(userId, conferenceId);
    }

    /**
     * 获取会议详情
     * @param conferenceId 会议ID
     * @return 参会议的所有人的数据，包括用户信息以及本次会议的情绪计数
     */
    @RequestMapping(value = "/getDetailByConferenceId", method = RequestMethod.GET)
    public Result getDetailByConferenceId(@RequestParam String conferenceId) {
        return conferenceDetailService.getDetailByConferenceId(conferenceId);
    }

    /**
     * 获取加入了某产会议的用户ID和用户名
     *
     * @param conferenceId 会议ID
     * @return 用户ID和用户名
     */
    @RequestMapping(value = "/getUserNameByConferenceId", method = RequestMethod.GET)
    public Result getUserNameByConferenceId(String conferenceId) {
        return conferenceDetailService.getUserNameByConferenceId(conferenceId);
    }

}
